package com.mjf.jst.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.jst.config.JSTConfig;
import com.mjf.jst.domain.ShopInfo;
import com.mjf.jst.enums.JSTApiEnum;
import com.mjf.jst.mapper.ShopInfoMapper;
import com.mjf.jst.service.JSTAsyncService;
import com.mjf.jst.service.ShopInfoService;
import com.mjf.jst.util.DateAndTimeUtils;
import com.mjf.jst.util.RequestUtil;
import com.mjf.jst.vo.ShopInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/7/6 21:17
 */
@Slf4j
@Service
public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> implements ShopInfoService {

    @Autowired
    private JSTAsyncService jstAsyncService;


    /**
     * 拉取店铺
     *
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void pullShopsList() {
        List<ShopInfoVO.ShopInfoDataList> shopInfoDataLists = jstAsyncService.pullShopsList();
        if (CollectionUtil.isNotEmpty(shopInfoDataLists)) {
            List<ShopInfo> shopInfos = new ArrayList<>();
            for (ShopInfoVO.ShopInfoDataList shopInfoDataList : shopInfoDataLists) {
                ShopInfo shopInfo = new ShopInfo();
                shopInfo.setShopId(shopInfoDataList.getShop_id());
                shopInfo.setShopName(shopInfoDataList.getShop_name());
                shopInfo.setCoId(shopInfoDataList.getCo_id());
                shopInfo.setShopSite(shopInfoDataList.getShop_site());
                shopInfo.setShopUrl(shopInfoDataList.getShop_url());
                if (ObjectUtil.isNotNull(shopInfoDataList.getCreated())) {
                    shopInfo.setCreated(DateAndTimeUtils.getDateTimeByStr(shopInfoDataList.getCreated().split("\\.")[0],
                        "yyyy-MM-dd HH:mm:ss"));
                }
                shopInfo.setNick(shopInfoDataList.getNick());
                if (ObjectUtil.isNotNull(shopInfoDataList.getSession_expired())) {
                    shopInfo.setSessionExpired(DateAndTimeUtils.getDateTimeByStr(shopInfoDataList.getSession_expired().split("\\.")[0],
                        "yyyy-MM-dd HH:mm:ss"));
                }
                shopInfo.setSessionUid(shopInfoDataList.getSession_uid());
                shopInfo.setShortName(shopInfoDataList.getShort_name());
                shopInfo.setGroupId(shopInfoDataList.getGroup_id());
                shopInfo.setGroupName(shopInfoDataList.getGroup_name());
                shopInfos.add(shopInfo);
            }
            saveOrUpdateBatch(shopInfos);
        }
        log.info("店铺数据同步完成");
    }

}
